class DpdController < ApplicationController  
  require 'open_flash_chart'
  
  def code_issue_summary_chart
    # [1] Get the session information.
    user = session[:user]
    password = session[:pass]
    dpd_host = session[:dpd_host] 
    project = session[:project] 
    timestamp = session[:timestamp]
    
    # [2] Get the data for the pie chart.
    dpd_consumer = DpdConsumer.new(dpd_host, 'codeissue', user, project, timestamp, password)
    code_issue_dpd = dpd_consumer.get_representation    
    tool_to_count = code_issue_dpd.get_tool_to_count

    # [3] Build the pie chart.
    links = []
    g = Graph.new
    g.pie(60, '#000000', '{font-size: 12px; color: #000000;}')
    g.pie_values(tool_to_count.values, tool_to_count.keys, links)
    g.pie_slice_colors(%w(#d01fc3 #356aa0 #c79810))
    g.set_tool_tip("#val# #x_label# Issues")
    g.title("Code Issues", '{font-size:15px; color: #000000}' )
    render :text => g.render
  end
  
  def unit_test_summary_chart
    # [1] Get the session information.
    user = session[:user]
    password = session[:pass]
    dpd_host = session[:dpd_host] 
    project = session[:project] 
    timestamp = session[:timestamp]
    
    # [2] Get the data for the chart.
    dpd_consumer = DpdConsumer.new(dpd_host, 'unittest', user, project, timestamp, password)
    unit_test_dpd = dpd_consumer.get_representation    
    success_count = unit_test_dpd.get_success_count.to_i
    failure_count = unit_test_dpd.get_failure_count.to_i + 50

    success_bar = BarOutline.new(50, '#006400', '#000000')
    success_bar.key("Unit Tests Successful", 10)
    failure_bar = BarOutline.new(50, '#b22222', '#000000')
    failure_bar.key("Unit Tests Failed", 10)

  
    g = Graph.new
    g.title("Unit Tests", "{font-size: 15px;}")

    success_bar.data << success_count
    g.data_sets << success_bar
    failure_bar.data << failure_count
    g.data_sets << failure_bar

    g.set_x_labels(%w(Tests))
    g.set_x_label_style(10, '#000000', 0,1)
    g.set_x_axis_steps(1)
    g.set_y_max(success_count + failure_count)
    g.set_y_label_steps(4)
    
    g.set_tool_tip("#val# #key#")
    render :text => g.render
  end
  
  def commit_summary_chart
    # [1] Get the session information.
    user = session[:user]
    password = session[:pass]
    dpd_host = session[:dpd_host] 
    project = session[:project] 
    timestamp = session[:timestamp]
    
    # [2] Get the data for the chart.
    dpd_consumer = DpdConsumer.new(dpd_host, 'commit', user, project, timestamp, password)
    commit_dpd = dpd_consumer.get_representation    
    commit_count = commit_dpd.get_data_count('Commits')
    
    lines_added_count = commit_dpd.get_data_count('LinesAdded')
    added_bar = BarOutline.new(50, '#006400', '#000000')
    added_bar.key("Lines Added", 10)
    added_bar.data << lines_added_count
    
    lines_deleted_count = commit_dpd.get_data_count('LinesDeleted')
    deleted_bar = BarOutline.new(50, '#b22222', '#000000')
    deleted_bar.key("Lines Deleted", 10)
    deleted_bar.data << lines_deleted_count

    g = Graph.new    
    g.data_sets << added_bar
    g.data_sets << deleted_bar
    
    #[3] Setup the graphic settings of the graph.
    g.title("Total Commits (" + commit_count.to_s + ")", "{font-size: 15px;}")    

    g.set_x_labels(['Commit Daily Project Data'])
    g.set_x_label_style(10, '#000000', 0,1)
    g.set_x_axis_steps(0)
    g.set_y_max(lines_added_count + lines_deleted_count)
    g.set_y_label_steps(4)
    
    g.set_tool_tip("#val# #key#")
    render :text => g.render
  end
  
  def build_summary_chart
    # [1] Get the session information.
    user = session[:user]
    password = session[:pass]
    dpd_host = session[:dpd_host] 
    project = session[:project] 
    timestamp = session[:timestamp]
    
    # [2] Get the data for the chart.
    dpd_consumer = DpdConsumer.new(dpd_host, 'build', user, project, timestamp, password)
    build_dpd = dpd_consumer.get_representation    
    
    success_count = build_dpd.get_data_count('success')
    success_bar = BarOutline.new(50, '#006400', '#000000')
    success_bar.key("Successes", 10)
    success_bar.data << success_count
    
    failure_count = build_dpd.get_data_count('failure')
    failure_bar = BarOutline.new(50, '#b22222', '#000000')
    failure_bar.key("Failures", 10)
    failure_bar.data << failure_count

    g = Graph.new    
    g.data_sets << success_bar
    g.data_sets << failure_bar
    
    #[3] Setup the graphic settings of the graph.
    total_builds = success_count + failure_count
    g.title("Total Builds(" + total_builds.to_s + ")", "{font-size: 15px;}")    

    g.set_x_labels(['Build Daily Project Data'])
    g.set_x_label_style(10, '#000000', 0,1)
    g.set_x_axis_steps(0)
    g.set_y_max(100)
    g.set_y_label_steps(4)
    
    g.set_tool_tip("#val# #key#")
    render :text => g.render
  end
  
  def dpd_summary 
    # Setup the webservice request parameters
    dpd_host = params[:host]
    if dpd_host == nil
      dpd_host = session[:dpd_host]
    end
    project = params[:project]
    
    # Create the timestamp string used to get the dpd info.  
    date_string = params[:calendar_date]
    session[:current_date] = date_string
    date = Date.strptime(date_string, "%B %d, %Y")
    
    year = date.year.to_s
    month = date.month.to_s
    day = date.day.to_s 
    
    # Append the leading 0's if they do not exist.
    if month.length == 1
      month = '0' + month 
    end 
    if day.length == 1
      day = '0' + day
    end
    timestamp =  year + '-'+  month+ '-' + day + 'T00:00:00.000'
    
    # Create the charts displayed in the view.
    session[:dpd_host] = dpd_host
    session[:project] = project
    session[:timestamp] = timestamp
    @code_issue_graph = open_flash_chart_object(400,250, 'code_issue_summary_chart', false) 
    @unit_test_graph = open_flash_chart_object(400,250, 'unit_test_summary_chart', false)
    @commit_graph = open_flash_chart_object(400,250, 'commit_summary_chart', false)    
    @build_graph = open_flash_chart_object(400,250, 'build_summary_chart', false)    
  end
  
  def save_projects
    user = params[:user]
    pass = params[:password]
    session[:user] = user
    session[:pass] = pass
    project_consumer =ProjectConsumer.new(params[:sensorbase_host], user, pass)
    projects = project_consumer.get_projects
    session[:projects] = projects
    redirect_to :action => 'dpd_selector', :projects => projects, :user => user, :pass => pass
  end
  
  def dpd_selector
  end
end
